package org.rbreznak.tronco.client;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;

import javax.swing.JLabel;
import javax.swing.Timer;

public class LoginScreen extends Screen{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private JLabel statusText = new JLabel();
	
	private Timer invalidTimer;
	
	private SwipeReader cr = new SwipeReader();
	
	public LoginScreen() {
		
		reset();
		
		invalidTimer = new Timer(3000,this);
		
		setLayout(new BorderLayout());
		setBackground(new Color(0,0,0));
		
		FormatUI.formatLabel(statusText);
		
		add(statusText, BorderLayout.CENTER);
		
		this.addKeyListener(this);
		
		this.setVisible(true);
		
		cr.setRead(true);
	}
	
	public void actionPerformed(ActionEvent arg0) {
		if(arg0.getSource() == invalidTimer) {
			invalidTimer.stop();
			reset();
			cr.setRead(true);
		}
	}

	public void keyPressed(KeyEvent e) {
		cr.keyTyped(e);
		
		if(cr.isEnd()) {
			invalidTimer.start();
			statusText.setText(cr.toString());
		}
	}

	public void keyReleased(KeyEvent e) {
		
	}

	public void keyTyped(KeyEvent e) {
		
	}

	@Override
	public void decideNext() {
		if(nodes.size() > 0) {
			setNext(nodes.get(0));
		}
	}
	
	@Override
	public String toString() {
		return statusText.getText();
	}

	@Override
	public void reset() {
		setNext(null);
		cr.clear();
		cr.setRead(true);
		statusText.setText("Please Swipe Your ID");
	}
}
